#include "Book.hpp"
#include "Patron.hpp"
#include "Data.hpp"
#include <vector>
#include <iostream>

struct Transaction
{
    Transaction() {}
    Transaction(Book Book, Patron patron, Data data) : _book(Book), _patron(patron), _data(data) {}
    Book _book;
    Patron _patron;
    Data _data;
};

class Library
{
public:
    Library() {}
    bool Add_Book();
    bool Add_Patron();
    bool Borrow_A_Book();

private:
    std::vector<Book> _books;
    std::vector<Patron> _patrons;
    std::vector<Transaction> _transactions;
};

bool Library::Add_Book()
{
    std::string name;
    std::string isbn;
    std::string author;
    int type;
    std::cout << "请输入书名：";
    std::cin >> name;
    std::cout << "请输入ISBN：";
    std::cin >> isbn;
    std::cout << "请输入作者：";
    std::cin >> author;
    std::cout << "请输入作品类型(数字)，作品类型如下：" << std::endl;
    std::cout << "0. fiction,1. nonfinction,2. periodical,3. biography,4. children" << std::endl;
    std::cin >> type;
    _books.push_back({isbn, name, author, static_cast<Genre>(type)});
    return true;
}

bool Library::Add_Patron()
{
    std::string name;
    std::cout << "请输入读者姓名：";
    std::cin >> name;
    std::string id;
    std::cout << "请输入读者图书证号：";
    std::cin >> id;
    _patrons.push_back({name, id});
    return true;
}

bool Library::Borrow_A_Book()
{
    std::string name;
    std::string book_name;
    Book book;
    Patron patron;
    std::cout << "请输入您的姓名：";
    getline(std::cin, name);
    bool flag = false;
    for (auto &e : _patrons)
    {
        if (name == e.GetName())
        {
            patron = e;
            flag = true;
            break;
        }
    }
    if (flag == false)
    {
        std::cerr << "没有您的姓名，请将您的姓名添加进图书馆内！" << std::endl;
        return false;
    }
    std::cout << "请输入书名：";
    getline(std::cin, book_name);
    flag = false;
    for (auto &e : _books)
    {
        if (book_name == e.GetName())
        {
            book = e;
            flag = true;
            break;
        }
    }
    if (flag == false)
    {
        std::cerr << "没有此书！" << std::endl;
        return false;
    }
    _transactions.push_back({book, patron, Data{2025, Month::Jul, 24}});
}